package svg.sample;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.net.*;

import javax.swing.*;

import org.apache.batik.swing.JSVGCanvas;

public class Quiz extends JDialog {

    private JSVGCanvas canvas;

    private JComboBox cboLetters;
    private JRadioButton rbtnHindi;
    private JRadioButton rbtnUrdu;
    private JButton btnExit;

    private int currLetterIndex;
    private String currLang;
    
    private String[] hindi_letters = {"a","aa","i","ii","u","uu","ksha","e","ai","o","au","am","ah", "ka","kha","ga","gha","cha","chah","ja","jha","naah","ta","tha","da","dha","nah","tah","thah","dah","dhah","na","pa","pha","ba", "bha","ma","yah","ra","la","va","sah","sha","sa","ha","ksa","tra","jna","sra"};
    private String[] urdu_letters = {"aih","alit","bari","be","casmi","che","choti","dal","dal2","feq","ghain","hamza","he","he2","jim","kap","khe","lam","mim","nun","pe","pe2","qat","re","re2","saf","sin","sin2","suad","te","te2","toe","vao","zal","ze","zhe","zhe2","zoe","zuad"};
    
    private int x1, y1, x, y;
    private boolean mouseButtonDown = false;

    public Quiz(JFrame owner, String title) {
        super(owner, title, true);

        canvas = new JSVGCanvas();

        //add mouse (e.g press, release) event handler
        canvas.addMouseListener(new MouseHandler());

        //add mouse motion (e.g mouse drag) event handler
        canvas.addMouseMotionListener(new MouseMotionHandler());

        //size of the dialog
        setSize(400, 300);

        // add the components to the container
        Container contentPane = getContentPane();
        contentPane.add(canvas, BorderLayout.CENTER);
        contentPane.add(createSouthPanel(), BorderLayout.SOUTH);
    }

    private JPanel createSouthPanel() {
        JPanel panel = new JPanel(new FlowLayout());

        rbtnHindi = new JRadioButton("Hindi");
        rbtnHindi.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                currLang = "Hindi";
                populateDropDown(hindi_letters);
             //   moveFirst(currLang);
            }
        });

        rbtnUrdu = new JRadioButton("Urdu");
        rbtnUrdu.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                currLang = "Urdu";
                populateDropDown(urdu_letters);
              //  moveFirst(currLang);
            }
        });

        JLabel lblCurrLetter = new JLabel("Current Letter");

        cboLetters = new JComboBox();
        cboLetters.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (cboLetters.getSelectedIndex() >= 0) {
                    currLetterIndex = cboLetters.getSelectedIndex();
                   // move(currLang);
                }
            }
        });
        
        btnExit = new JButton("Exit");
        btnExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        
        ButtonGroup langGroup = new ButtonGroup();
        langGroup.add(rbtnHindi);
        langGroup.add(rbtnUrdu);

        panel.add(rbtnHindi);
        panel.add(rbtnUrdu);
        panel.add(lblCurrLetter);
        panel.add(cboLetters);
        panel.add(btnExit);

        return panel;
    }

    private URL getURL(String letter) {
       
        java.io.File file = new File(letter);
        URL url = null;
        try {
          url = file.toURL();
        }
        catch (MalformedURLException ex) {
        }
        return url;
    }
    
    private void loadLetter(String language, String letter) {
        String svgFile = getSVGFilePath(language, letter);
        URL url = getURL(svgFile);
        canvas.loadSVGDocument(url.toString());
    }

    private String getLetter(String language, int index) {
        String letter = null;

        if (language.equals("Hindi"))
            letter = hindi_letters[index];

        if (language.equals("Urdu"))
            letter = urdu_letters[index];

        return letter;
    }

    private String getSVGFilePath(String language, String letter) {
        String langDir = "languages/" + language;
        String svgFile = langDir + "/" + letter + ".svg";
        return svgFile;
    }
    
    private void populateDropDown(String[] letters) {
        cboLetters.removeAllItems();
        for (int i = 0; i < letters.length; i++) {
            cboLetters.addItem(letters[i]);
        }
    }
    
    private int getLastLetterIndex(String language) {
        int lastLetterIndex = 0;

        if (language.equals("Hindi"))
            lastLetterIndex = hindi_letters.length - 1;

        if (language.equals("Urdu"))
            lastLetterIndex = urdu_letters.length - 1;

        return lastLetterIndex;
    }
    
    class MouseHandler implements MouseListener {

        public void mouseClicked(MouseEvent e) {
            //this is how u get x and y coordinates...simple and sweet
            //System.out.println(e.getX() + ":" + e.getY());
        }

        public void mouseEntered(MouseEvent e) {}

        public void mouseExited(MouseEvent e) {}

        public void mousePressed(MouseEvent e) {
            x1 = e.getX();
            y1 = e.getY();
            mouseButtonDown = true;
        }

        public void mouseReleased(MouseEvent e) {
            mouseButtonDown = false;
        }
    }

    class MouseMotionHandler implements MouseMotionListener {
        public void mouseDragged(MouseEvent e) {
            if (mouseButtonDown) {
                Graphics2D g = (Graphics2D)canvas.getGraphics();
                x = e.getX();
                y = e.getY();
                //make the line have thickness and the color red
                Stroke stroke = new BasicStroke(2.0f);
                g.setColor(Color.red);
                g.setStroke(stroke);
                g.drawLine(x1, y1, x, y);
                x1 = x;
                y1 = y;
            }
        }

        public void mouseMoved(MouseEvent e) {}
    }

}
